package com.cmcc.cmii.smspush.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Thanks
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public T queryById(Long id) {
        return this.mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @Override
    public List<T> queryAll() {
        return this.mapper.selectList(null);
    }

    /**
     * 查询满足条件的所有数据
     */
    @Override
    public List<T> queryAllByWrapper(QueryWrapper<T> wrapper){
        return this.mapper.selectList(wrapper);
    }

    /**
     * 根据条件查询一条数据
     *
     * @param record
     * @return
     */
    @Override
    public T queryOne(T record) {
        return this.mapper.selectOne(new QueryWrapper<>(record));
    }

    /**
     * 查询满足条件的一条记录
     */
    @Override
    public T queryOneByWrapper(Wrapper<T> wrapper){
        return this.mapper.selectOne(wrapper);
    }

    /**
     * 根据条件查询数据列表
     *
     * @param record
     * @return
     */
    @Override
    public List<T> queryListByWhere(T record) {
        return this.mapper.selectList(new QueryWrapper<>(record));
    }



    /**
     * 根据条件分页查询数据列表
     *
     * @param record
     * @return
     */
    @Override
    public IPage<T> queryPageListByWhere(IPage<T> page, T record) {
        try{
            // 获取分页数据
            return this.mapper.selectPage(page, new QueryWrapper<>(record));
        }catch (Exception e){
            e.printStackTrace();
        }

        return new Page<>(1,10);


    }

    /**
     * 根据对象外的条件查询
     */
    @Override
    public IPage<T> queryPageByWrapper(IPage<T> page, QueryWrapper<T> wrapper){
        try{
            // 获取分页数据
            return this.mapper.selectPage(page, wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }

        return new Page<>(1,10);
    }

    /**
     *
     */

    /**
     * 保存数据
     *
     * @param record
     * @return
     */
    @Override
    @Transactional
    public Integer save(T record) {
        return this.mapper.insert(record);
    }

    /**
     * 更新数据
     *
     * @param record
     * @return
     */
    @Override
    @Transactional
    public Integer update(T record) {
        return this.mapper.updateById(record);
    }


    /**
     *
     * @param record
     * @param updateWrapper
     * @return
     */
    @Override
    public Integer updateWrapper(T record, UpdateWrapper<T> updateWrapper){
        return this.mapper.update(record,updateWrapper);
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Integer deleteById(Long id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 根据ids批量删除数据
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Integer deleteByIds(List<Long> ids) {
        return this.mapper.deleteBatchIds(ids);
    }

    /**
     * 根据条件删除数据
     *
     * @param record
     * @return
     */
    @Override
    @Transactional
    public Integer deleteByWhere(T record) {
        return this.mapper.delete(new QueryWrapper<>(record));
    }

    /**
     * 根据条件删除
     * @param wrapper
     * @return
     */
    @Override
    @Transactional
    public Integer deleteByWrapper(QueryWrapper<T> wrapper) {
        return this.mapper.delete(wrapper);
    }


    public BaseMapper<T> getMapper(){
        return this.mapper;
    }


}
